using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;

namespace FiltronASP.Modules
{
    public class languageChangerSublayoutModule : AbstractModule
    {
        /// <summary>
        /// Klasa odpowiedzialna za generowanie contentu przez modul.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>content</returns>
        public override string GenerateContent(FiltronASP.Models.Page page)
        {
            string htmlToReturn = "";
            
         //   htmlToReturn += "<div id=\"languageChangerContainer\" >";
            htmlToReturn += "	<select onchange=\"changeLocationDD(this);\" id=\"languageChangerList\"  class=\"language\" style='right: 0; top: 77px; margin-right: 0; background-color: #141414; border-color: #141414;'>";
            ILanguageDAO LanguageDAO = new LanguageDAO();
            Language currentLanguage = LanguageDAO.FindByPK(page.language_id);
            htmlToReturn += String.Format("<option title=\"Content/images/{1}\" selected=\"selected\" value=\"-1\">{0}</option>", currentLanguage.language_abbr.ToUpper(), currentLanguage.language_img);
            //htmlToReturn += "		<option title="Content/images/flag_pl.jpg" value=\"1\">Język polski</option>";

            IQueryable<Language> languagesCollection = LanguageDAO.FindAll();
            if (languagesCollection != null)
            {
                languagesCollection = languagesCollection.OrderBy(l => l.language_created);
                if (languagesCollection != null)
                {
                    foreach (Language singleLanguage in languagesCollection)
                    {
                        if (singleLanguage.language_active != null)
                        {
                            if ((bool)singleLanguage.language_active)
                            {
                                if (singleLanguage.language_id != page.language_id)
                                {
                                    string currentPageLink = this.getMainPageByLanguageId(Convert.ToInt32(singleLanguage.language_id));
                                    htmlToReturn += String.Format("<option title=\"Content/images/{2}\" value=\"{0}\">{1}</option>", currentPageLink, singleLanguage.language_abbr.ToUpper(), singleLanguage.language_img);
                                }
                            }
                        }
                    }
                }   //if
            }

            htmlToReturn += "	</select>";
          //  htmlToReturn += "</div>";

            return htmlToReturn;
        }

        private string changeCountryName(string orgCountryName) {
            if (orgCountryName != null)
            {
                switch (orgCountryName.ToLower())
                {
                    case "hungary":
                        return "Magyarország";
                    case "italiano":
                        return "Italia";
                    case "russia":
                        return "Россия";
                    case "ukrainian":
                        return "Україна";
                    case "english":
                        return "United Kingdom";
                    case "polish":
                        return "Polska";
                    case "german":
                        return "Deutschland";
                    case "spanish":
                        return "España";
                    case "french":
                        return "France";
                    default:
                        return UppercaseFirst(orgCountryName);
                }
            }   //if
            return String.Empty;
        }

        private string UppercaseFirst(string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
        }

        private string getMainPageByLanguageId(int languageId)
        {
            if (languageId.Equals(0))
            {
                return null;
            }
            IPageDAO PageDAO = new PageDAO();
            ITemplateDAO TemplateDAO = new TemplateDAO();
            Template mainTemplate = TemplateDAO.FindByLangAndName(languageId, "tpl_mainPage");
            if (mainTemplate != null)
            {
                int mainTemplateId = mainTemplate.template_id;
                IQueryable<Page> currentPage = PageDAO.FindByLanguageAndTemplateIdAndPublished(languageId, mainTemplateId, true);
                if (currentPage != null)
                {
                    foreach (Page singlePage in currentPage)
                    {                        
                        return singlePage.page_link;
                    }
                }
            }
            return null;
        }
    }
}
